home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / hzip.com / HZIP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-08  |  1.3 KB  |  59 lines

  1. ////////////////////////////////////////////////////////
  2. // hzip.cpp: Huffman file compressor
  3. // Copyright (c) 1991 Azarona Software
  4. // All rights reserved. 
  5. ////////////////////////////////////////////////////////
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include "huffenc.h"
  10.  
  11. huff_encoder huff(256, 16); // 256 symbols, 16 bit max code lengths
  12.  
  13. main(int argc, char *argv[])
  14. {
  15.   FILE *f, *g;
  16.  
  17.   if (argc < 3) {
  18.      printf("Usage: hzip infile outfile\n");
  19.      return 0;
  20.   }
  21.   
  22.   f = fopen(argv[1], "rb");
  23.   if (f == NULL) {
  24.      printf("Couldn't open input file: %s\n", argv[1]);
  25.      return 0;
  26.   }
  27.  
  28.   g = fopen(argv[2], "wb");
  29.   if (g == NULL) {
  30.      printf("Couldn't create output file: %s\n", argv[2]);
  31.      return 0;
  32.   }
  33.  
  34.   printf("Collecting frequencies ...\n");
  35.   while(!feof(f)) {
  36.     int c = fgetc(f);
  37.     if (c != -1) huff.counts[c]++;
  38.   }
  39.  
  40.   printf("Generating huffman codes ...\n");
  41.   if (!huff.generate_codes()) {
  42.      printf("No solution to package-merge. Codes could not "
  43.             "be generated.\n");
  44.      exit(1);
  45.   }
  46.  
  47.   huff.print_data(1);
  48.  
  49.   fputs("HZIP 1.0", g);
  50.   huff.dump_codes(g, 8); // 8 is sizeof(signature)
  51.  
  52.   printf("Compressing file ...\n");
  53.   fseek(f, 0, SEEK_SET);
  54.   huff.encode(g, f);
  55.  
  56.   fclose(f);
  57.   fclose(g);
  58. }
  59.